﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace SYH.Utils.Mail
{
    public class SendMail
    {
        #region 发件箱参数
        private string _host;
        private string _port;
        private string _name;
        private string _pass;

        /// <summary>
        /// 主机名
        /// </summary>
        public string Host
        {
            get { return _host; }
            set { _host = value; }
        }
        /// <summary>
        /// 端口号
        /// </summary>
        public string Port
        {
            get { return _port; }
            set { _port = value; }
        }
        /// <summary>
        /// 发件箱
        /// </summary>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        /// <summary>
        /// 密码
        /// </summary>
        public string Pass
        {
            get { return _pass; }
            set { _pass = value; }
        }
        #endregion

        public SendMail(string Name)
        {
            this.Name = Name;
        }

        public SendMail(string Host, string Port, string Name, string Pass)
        {
            this.Host = Host;
            this.Port = Port;
            this.Name = Name;
            this.Pass = Pass;
        }

        #region 邮箱发送验证码
        /// <summary>
        /// 邮箱发送验证码
        /// </summary>
        /// <param name="MessageTo">收件箱</param>
        /// <param name="randCode">验证码</param>
        /// <returns></returns>
        public bool SendCode(string MessageTo, string randCode)
        {
            bool result = false;
            MailAddress MessageFrom = new MailAddress(Name); //发件人邮箱地址            
            string MessageSubject = "生意汇用户找回密码邮件"; //邮件主题
            string MessageBody = "验证码为：" + randCode; //邮件内容 
            if (Send(MessageTo, MessageSubject, MessageBody))
                result = true;

            return result;
        }
        #endregion

        #region 发送邮件
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="MessageFrom">发件人邮箱地址</param>
        /// <param name="MessageTo">收件人邮箱地址</param>
        /// <param name="MessageSubject">邮件主题</param>
        /// <param name="MessageBody">邮件内容（一般是一个网址链接，生成随机数加验证id参数，点击去网站验证。）</param>
        /// <returns></returns>
        public bool Send(string MessageTo, string MessageSubject, string MessageBody)
        {
            MailAddress mailAddress = new MailAddress(Name);
            MailMessage message = new MailMessage();
            message.From = mailAddress; //发件地址
            message.To.Add(MessageTo); //收件人邮箱地址可以是多个以实现群发
            message.Subject = MessageSubject;
            message.Body = MessageBody;
            message.IsBodyHtml = false; //是否为html格式
            message.Priority = MailPriority.High; //发送邮件的优先等级

            SmtpClient sc = new SmtpClient();
            sc.Host = Host; //指定发送邮件的服务器地址或IP
            sc.Port = int.Parse(Port); //指定发送邮件端口
            sc.UseDefaultCredentials = true;
            sc.Credentials = new System.Net.NetworkCredential(Name, Pass); //指定登录服务器的用户名和密码(发件人的邮箱登陆密码)

            try
            {
                sc.Send(message); //发送邮件
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }
        #endregion

        /// <summary>
        /// 注册发送邮箱验证码
        /// </summary>
        /// <param name="Email"></param>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static bool SendCheckMailSMS(string Email, string Key)
        {
            string content = "您正在进行绑定邮箱操作，您的验证码是：{0}。";
            content = string.Format(content, Key);

            return Common.EmailHelper.SendEmail(Email, "[生意汇]邮箱绑定", content, "生意汇");

        }
    }
}
